<?php
// +----------------------------------------------------------------------
// | CoreThink [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.corethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com> <http://www.corethink.cn>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Think\Controller;
/**
 * 后台用户控制器
 * @author jry <598821125@qq.com>
 */
class CustomerController extends AdminController{
	 /**
     * 客户信息列表
     * @author jry <598821125@qq.com>
     */
	public function index(){
		 //搜索
	//	 header('content-type:text/html;charset=utf-8');
		
        $keyword = I('keyword', '', 'string');
        $condition = array('like','%'.$keyword.'%');
        $map['customer_num|customer_name|mobile'] = array($condition, $condition, $condition,'_multi'=>true);
       $keywords=I('keywords');
	  
	  if(!empty($keywords)){
	  	$map['level']=$keywords;
	  }
        //获取所有用户
       $map['status'] = array('egt', '0'); //禁用和正常状态
        $data_list = D('Customer')->page(!empty($_GET["p"])?$_GET["p"]:1, C('ADMIN_PAGE_ROWS'))->where($map)->order('id desc')->select();
        $page = new \Common\Util\Page(D('Customer')->where($map)->count(), C('ADMIN_PAGE_ROWS' ));

			foreach($data_list as $key=>&$val){
			$val['customer_service_id_text']=get_employee_info($val['customer_service_id'],'name');
			$tt['tablename']='customer_level';
			$tt['fieldvalue']=$val['level'];
			$customer_level=D('Descriptions')->where($tt)->find();
			$val['level']=$customer_level['title'];
			$rr['tablename']='customer_source';
			$rr['fieldvalue']=$val['source'];
			$customer_source=D('Descriptions')->where($rr)->find();
			$val['source']=$customer_source['title'];
			$val['city_text']=get_city_name($val['province']).'-'.get_city_name($val['city']);
			$tm['id']=$val['city'];
			$val['city']=D('Region')->where($tm)->getField('name' );
			$val['customer_name']='<a href="'.U('Customer/xq',array('id'=>$val['id'])).'"style="text-decoration:none;">'.$val['customer_name'].'</a>';
				}

				$attr['title'] = '添&nbsp;项';			
				$attr['class'] = 'label label-xq';			
				$attr['href']  = U('Admin/Project/add', array('customerid' => '__data_id__'));				
 
                $attre['title'] = '编';			
				$attre['class'] = 'label label-primary';			
				$attre['href']  = U('Admin/Customer/edit', array('id' => '__data_id__'));	

                $attrf['title'] = '服';			
				$attrf['class'] = 'label label-tf';			
				$attrf['href']  = U('Admin/CustomerService/index', array('customerid' => '__data_id__'));
 
                 $attrx['title'] = '项';			
				$attrx['class'] = 'label label-tx';			
				$attrx['href']  = U('Admin/Project/index', array('customerid' => '__data_id__'));
			
				$customer_level=D('Descriptions')->where("tablename='customer_level'")->select();
	
        //使用Builder快速建立列表页面。
        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('客户信息列表') //设置页面标题
                ->addTopButton('addnew')  //添加新增按钮
           //     ->addTopButton('resume')  //添加启用按钮
            //    ->addTopButton('forbid')  //添加禁用按钮
                ->addTopButton('delete')  //添加删除按钮
              
              ->addTopButton('self',array('title' => '我的客户','href' => U('Admin/customer/my') )) 
          
              
              
                ->setSearch('请输入客户名/客户编号/手机号', U('index'),$customer_level)
              //  ->addTableColumn('id', '客户ID')
               //->addTableColumn('customer_num', '客户编号')
                ->addTableColumn('level', '级别')
                ->addTableColumn('customer_name', '客户名')		
				->addTableColumn('qq','QQ')				
				->addTableColumn('mobile','手机号')				
				->addTableColumn('city_text','城市')				
				->addTableColumn('customer_service_id_text','客服')
				->addTableColumn('source','来源')
         //		->addTableColumn('sex','性别')
		//		->addTableColumn('company','客户公司名')
		//		->addTableColumn('wechat','微信号')
		//		->addTableColumn('phone','办公电话')
		//        ->addTableColumn('email', '邮箱')
		//		->addTableColumn('province','省份')
		//		->addTableColumn('address','地址')
		//		->addTableColumn('parent_customerid','父客户id')
		//		->addTableColumn('recommended','推荐人')
		//		->addTableColumn('create_time','创建时间','time')
		//		->addTableColumn('create_member','创建人ID')
		//		->addTableColumn('remark','备注')
		//		->addTableColumn('visit_times','回访次数')
		//		->addTableColumn('service_times','投诉次数')
		//		->addTableColumn('last_visit_time','上次回访时间')
				->addTableColumn('last_service_time','上次服务时间','date')
		//		->addTableColumn('status', '状态', 'status')
	             ->addTableColumn('right_button', '操作', 'btn')
                ->setTableDataList($data_list) //数据列表
                ->setTableDataPage($page->show()) //数据列表分页
                ->addRightButton('self',$attre)   //添加编辑按钮
                 ->addRightButton('self',$attrf) //添加服务搜索按钮
                ->addRightButton('self',$attrx) //添加项目搜索按钮
                 ->addRightButton('self',array('title'=>'添&nbsp;服','class'=>'label label-fw','href'=>U('Admin/customerService/add', array('customerid' => '__data_id__'))))   
               ->addRightButton('self',$attr) //添加客户详情按钮
                ->display();
		
	}
		
	//新增客户信息
	public function add(){

		$customer_level=D('Descriptions')->where('tablename="customer_level"')->field('fieldvalue as id,title')->select();
		$customer_source=D('Descriptions')->where('tablename="customer_source"')->field('fieldvalue as id,title')->select();
	//var_dump($customer_level);exit;
		 $id=is_login();
		 $username=get_user_info($id);
		if(IS_POST){
            $customer_object = D('Customer');
            $data = $customer_object->create();
            if($data){
                $id = $customer_object->add();
                if($id){
                    $this->success('新增成功', U('index'));
                }else{
                    $this->error('新增失败');
                }
            }else{
                $this->error($customer_object->getError());
            }
        }else{
        	$kefu_arr=M("employee")->where("department=7")->field("uid as id,name as title")->select();
            //使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('新增客户') //设置页面标题
                    ->setPostUrl(U('add')) //设置表单提交地址
               
                   // ->addFormItem('customer_num','text', '客户编号', '客户编号')
		            ->addFormItem('level','select2', '客户级别', '客户级别',$customer_level)
					->addFormItem('source','select2','来源','来源',$customer_source) 
		            ->addFormItem('customer_name','text', '客户名', '客户名')
					->addFormItem('sex','radio','客户性别','客户性别',C('USER_SEX_LIST'))
					->addFormItem('company','text','客户公司名','客户公司名')
					->addFormItem('qq','text','QQ','QQ')
					->addFormItem('wechat','text','微信号','微信号')
					->addFormItem('phone','text','办公电话','办公电话')
					->addFormItem('mobile','text','手机号','手机号')
		            ->addFormItem('email','text', '邮箱', '邮箱') 
					->addFormItem('province','selects','省份','省份')  
					
					->addFormItem('address','text','地址','地址') 
					->addFormItem('customer_service_id','select2','客服','客服',$kefu_arr)
					->addFormItem('recommended','text','推荐人','推荐人')  
					->addFormItem('parent_customerid','customer','父客户编号','父客户id')   
//					->addFormItem('create_time','hidden','创建时间','创建时间') 
 					->addFormItem('create_member','hidden','创建人ID','创建人ID')  
					->addFormItem('remark','textarea','备注','备注')
//					->addFormItem('visit_times','text','回访次数','回访次数')
//					->addFormItem('service_times','text','投诉次数','投诉次数')
//					->addFormItem('last_visit_time','text','上次回访时间','上次回访时间')
					->addFormItem('last_service_time','hidden','上次服务时间','上次服务时间')
                
                    ->setFormData(array('customer_service_id' => $username['username'],'create_member'=>$id,'last_service_time'=>NOW_TIME)) //注册方式为后台添加
                    ->display();
        }
	}

    /**
     * 编辑
     * @author jry <598821125@qq.com>
     */
    public function edit($id){
    	$customer_level=D('Descriptions')->where('tablename="customer_level"')->field('fieldvalue as id,title')->select();
	    $customer_source=D('Descriptions')->where('tablename="customer_source"')->field('fieldvalue as id,title')->select();
		        	$kefu_arr=M("employee")->where("department=7")->field("uid as id,name as title")->select();
        //获取信息
        $info = D('Customer')->find($id);

        if(IS_POST){
            $customer_object = D('Customer');
            $customer_object->create();
            if($customer_object->save()){
            	
                $this->success('更新成功', U('index'));
            }else{
                
                $this->error('更新失败', $customer_object->getError());
            }
        }else{
            $info = D('Customer')->find($id);
            //使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('编辑') //设置页面标题
                    ->setPostUrl(U('edit')) //设置表单提交地址
                    ->addFormItem('id', 'hidden', 'ID', 'ID')
	
                 //   ->addFormItem('usertype', 'radio', '用户类型', '用户类型', C('USER_TYPE_LIST'))
                    ->addFormItem('customer_num','text', '客户编号', '客户编号')
		            ->addFormItem('level','select2', '客户级别', '客户级别',$customer_level)
		            ->addFormItem('customer_name','text', '客户名', '客户名')
					->addFormItem('sex','radio','客户性别','客户性别',C('USER_SEX_LIST'))
					->addFormItem('company','text','客户公司名','客户公司名')
					->addFormItem('qq','text','QQ','QQ')
					->addFormItem('wechat','text','微信号','微信号')
					->addFormItem('phone','text','办公电话','办公电话')
					->addFormItem('mobile','text','手机号','手机号')
		            ->addFormItem('email','text', '邮箱', '邮箱') 

					->addFormItem('province','selects','省份','省份')  
				// ->addFormItem('city','text','城市','城市') 


					->addFormItem('address','text','地址','地址') 
					->addFormItem('parent_customerid','text','父客户id','父客户id')   
					->addFormItem('recommended','text','推荐人','推荐人')  
					->addFormItem('source','select2','来源','来源',$customer_source) 
					->addFormItem('customer_service_id','select2','客服id','客服id',$kefu_arr)
//					->addFormItem('create_time','hidden','创建时间','创建时间') 
//					->addFormItem('create_member','text','创建人ID','创建人ID')  
					->addFormItem('remark','textarea','备注','备注')
//					->addFormItem('visit_times','text','回访次数','回访次数')
//					->addFormItem('service_times','text','投诉次数','投诉次数')
//					->addFormItem('last_visit_time','text','上次回访时间','上次回访时间')
//					->addFormItem('last_service_time','hidden','上次服务时间','上次服务时间')
					 
                    ->setFormData($info)
                    ->display();
        }
    }
 //客户详情
 public function xq($id){
	$map['id']=$id;
	$data_list = D('Customer')->page(!empty($_GET["p"])?$_GET["p"]:1, C('ADMIN_PAGE_ROWS' ))->where($map)->select();
	foreach($data_list as $key=>&$val){
	$val['customer_service_id_text']=get_employee_info($val['customer_service_id'],'name');
	$tt['tablename']='customer_level';
	$tt['fieldvalue']=$val['level'];
	$customer_level=D('Descriptions')->where($tt)->find();
	$val['level']=$customer_level['title'];
	$rr['tablename']='customer_source';
	$rr['fieldvalue']=$val['source'];
	$customer_source=D('Descriptions')->where($rr)->find();
	$val['source']=$customer_source['title'];
	$val['city_text']=get_city_name($val['province']).get_city_name($val['city']).$val['address'];
	$tm['id']=$val['city'];
	$val['city']=D('Region')->where($tm)->getField('name');
	$val['create_member']=get_employee_info($val['create_member'],'name');
	$arr=D('Customer')->where($map)->find();
	$val['head_one']=$arr['customer_name'].'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.date('Y/m/d',$arr['create_time']).'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$val['level'];
	switch($val['sex']){
	case '1':
	$val['sex']="男";
	break;
	case '-1':
	$val['sex']="女";
	break;
	case '0':
	$val['sex']="保密";
	break;
	}
	}
	//使用Builder快速建立列表页面。
	$builder = new \Common\Builder\ListBuilderxq();
	$builder->setMetaTitle('客户详情') //设置页面标题
	->addTableGroup('zu_one','head_one','客户','1')
	->addTableItem('zu_one','company','客户公司名','4')
	->addTableItem('zu_one','mobile','手机号','4')
	->addTableItem('zu_one','qq','QQ','4')
	->addTableItem('zu_one','email', '邮箱','4')
	->addTableItem('zu_one','sex','性别','4')
	->addTableItem('zu_one','phone','办公电话','4')
	->addTableItem('zu_one','wechat','微信号','4')
	->addTableItem('zu_one','city_text','城市','4')
	->addTableGroup('zu_two','head_two','其他信息','1')
	->addTableItem('zu_two','source','来源','4')
	->addTableItem('zu_two','customer_service_id_text','客服','4')
	->addTableItem('zu_two','create_member','创建人','4')
	->addTableItem('zu_two','last_visit_time','上次回访时间','4')
	->addTableGroup('zu_three','remark','备注','2')
	//	->addTableItem('zu_three','remark','备注')
	->setTableDataList($data_list) //数据列表
	->display();

	}
public function my(){
	$uid=is_login();
	 $keyword = I('keyword', '', 'string');
        $condition = array('like','%'.$keyword.'%');
        $map['customer_num|customer_name|mobile'] = array($condition, $condition, $condition,'_multi'=>true);
		$where['customer_service_id']=$uid;
		$where['create_member']=$uid;
		$where['_logic'] = 'or';
		$map['_complex']=$where;
		$keywords=I('keywords');
		if(!empty($keywords)){
	  	$map['level']=$keywords;
	  }
		        $attre['title'] = '编';			
				$attre['class'] = 'label label-primary';			
				$attre['href']  = U('Admin/Customer/edit', array('id' => '__data_id__'));	

                $attrf['title'] = '服';			
				$attrf['class'] = 'label label-tf';			
				$attrf['href']  = U('Admin/CustomerService/index', array('customerid' => '__data_id__'));
 
                 $attrx['title'] = '项';			
				$attrx['class'] = 'label label-tx';			
				$attrx['href']  = U('Admin/Project/index', array('customerid' => '__data_id__'));
		
	 $data_list = D('Customer')->page(!empty($_GET["p"])?$_GET["p"]:1, C('ADMIN_PAGE_ROWS'))->where($map)->order('id desc')->select();
        $page = new \Common\Util\Page(D('Customer')->where($map)->count(), C('ADMIN_PAGE_ROWS' ));

			foreach($data_list as $key=>&$val){
			$val['customer_service_id_text']=get_employee_info($val['customer_service_id'],'name');
			$tt['tablename']='customer_level';
			$tt['fieldvalue']=$val['level'];
			$customer_level=D('Descriptions')->where($tt)->find();
			$val['level']=$customer_level['title'];
			$rr['tablename']='customer_source';
			$rr['fieldvalue']=$val['source'];
			$customer_source=D('Descriptions')->where($rr)->find();
			$val['source']=$customer_source['title'];
			$val['city_text']=get_city_name($val['province']).'-'.get_city_name($val['city']);
			$tm['id']=$val['city'];
			$val['city']=D('Region')->where($tm)->getField('name' );
			$val['customer_name']='<a href="'.U('Customer/xq',array('id'=>$val['id'])).'"style="text-decoration:none;">'.$val['customer_name'].'</a>';
				}

				$attr['title'] = '添&nbsp;项';			
				$attr['class'] = 'label label-xq';
			
				$attr['href']  = U('Admin/Project/add', array('customerid' => '__data_id__'));
				
				$customer_level=D('Descriptions')->where("tablename='customer_level'")->select();
        //使用Builder快速建立列表页面。
        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('客户信息列表') //设置页面标题
                ->addTopButton('addnew')  //添加新增按钮
               // ->addTopButton('resume')  //添加启用按钮
               // ->addTopButton('forbid')  //添加禁用按钮
                ->addTopButton('delete')  //添加删除按钮
                 ->addTopButton('self',array('title' => '我的客户','href' => U('Admin/customer/my') )) 
                ->setSearch('请输入客户名/客户编号/手机号', U('my'),$customer_level)
              //  ->addTableColumn('id', '客户ID')
                //->addTableColumn('customer_num', '项目编号')
                ->addTableColumn('level', '级别')
                ->addTableColumn('customer_name', '客户名')		
				->addTableColumn('qq','QQ')				
				->addTableColumn('mobile','手机号')				
				->addTableColumn('city_text','城市')				
				->addTableColumn('customer_service_id_text','客服')
				->addTableColumn('source','来源')
         //		->addTableColumn('sex','性别')
		//		->addTableColumn('company','客户公司名')
		//		->addTableColumn('wechat','微信号')
		//		->addTableColumn('phone','办公电话')
		//        ->addTableColumn('email', '邮箱')
		//		->addTableColumn('province','省份')
		//		->addTableColumn('address','地址')
		//		->addTableColumn('parent_customerid','父客户id')
		//		->addTableColumn('recommended','推荐人')
		//		->addTableColumn('create_time','创建时间','time')
		//		->addTableColumn('create_member','创建人ID')
		//		->addTableColumn('remark','备注')
		//		->addTableColumn('visit_times','回访次数')
		//		->addTableColumn('service_times','投诉次数')
		//		->addTableColumn('last_visit_time','上次回访时间')
				->addTableColumn('last_service_time','上次服务时间')
		//		->addTableColumn('status', '状态', 'status')
	             ->addTableColumn('right_button', '操作', 'btn')
                ->setTableDataList($data_list) //数据列表
                ->setTableDataPage($page->show()) //数据列表分页
                ->addRightButton('self',$attre)   //添加编辑按钮
               ->addRightButton('self',$attrf) //添加禁用/启用按钮
                 ->addRightButton('self',$attrx) //添加删除按钮
                 ->addRightButton('self',array('title'=>'添&nbsp;服','class'=>'label label-fw','href'=>U('Admin/customerService/add', array('customerid' => '__data_id__'))))  //添加删除按钮
                ->addRightButton('self',$attr) //添加客户详情按钮
                ->display();
}




























	}
	